package mess;

import java.lang.reflect.Array;
import java.util.Arrays;

public class LeeCode_34 {
    public static void main(String[] args) {
        int[] nums = new int[]{2,2};
        System.out.println(Arrays.toString(searchRange(nums, 2)));
    }

    private static int[] searchRange(int[] nums, int target) {
        Arrays.sort(nums);
        int n = nums.length;
        if (n == 0)
            return new int[]{-1, -1};
        int l = 0;
        int r = n - 1;

        while (l < r){
            int mid = (l + r) / 2;
            if (target <= nums[mid])
                r = mid;
            else
                l = mid + 1;
        }

        int num = nums[l] == target ? nums[l] : -1;
        if (num == -1)
            return new int[]{-1, -1};

       /* int start = l;
        int count = 0;
        for (int i = l + 1; i < n; i++) {
            if (nums[i] != target)
                break;
            count++;
        }
        return new int[]{start, start + count};*/

        r = n - 1;
        int start = l;
        while (l < r){
            if (r - l == 1)
                if (nums[r] != target)
                    r--;
                else if (nums[r] == target)
                    l++;
            int mid = (l + r) / 2;
            if (nums[mid] == target)
                l = mid;
            else
                r = mid - 1;
        }
        return new int[]{start, r};
    }
}
